//package com.fortune.conf;
//
//import io.github.bonigarcia.wdm.WebDriverManager;
//import org.jsoup.Jsoup;
//import org.jsoup.nodes.Document;
//import org.jsoup.nodes.Element;
//import org.jsoup.select.Elements;
//import org.openqa.selenium.*;
//import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.chrome.ChromeOptions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
//
//import java.time.Duration;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Set;
//import java.util.concurrent.TimeUnit;
//
//public class ChromeDriverUtil_wanzneg {//此类 列表页开始查询，点击后去详情，拿到数据，已经完成，图片还没有下载完成
//
//    // 获取 WebDriver 实例
//    public WebDriver getWebDriver() {
//        ChromeOptions options = new ChromeOptions();
//        String driverPath = getDriverPath(options);
//        System.setProperty("webdriver.chrome.driver", driverPath);
//        // 添加必要的启动参数
////        options.addArguments("--headless=new");
//        options.addArguments("--remote-allow-origins=*");
//        options.addArguments("--no-sandbox");
//        options.addArguments("--disable-extensions");
//        options.addArguments("--disable-gpu");
//        options.addArguments("--disable-dev-shm-usage");
//        // 根据需要添加其他参数
//        WebDriver driver = new ChromeDriver(options);
//        // 设置最长等待时间
//        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//        return driver;
//    }
//
//    // 获取 chromedriver.exe 的路径
//    public static String getDriverPath(ChromeOptions options) {
//        // 请替换为实际的 chromedriver.exe 路径
//        return "E:/chromedriver-win64-135.95/chromedriver-win64/chromedriver.exe";
//    }
//    // 关闭当前窗口
//    public static void closeWindow(WebDriver driver) {
//        // 获取所有句柄的集合
//        List<String> winHandles = new ArrayList<>(driver.getWindowHandles());
//        if (!winHandles.isEmpty()) {
//            driver.switchTo().window(winHandles.get(0));
//            driver.close();
//            System.out.println("————————————————————————————————页面关闭————————————————————————————————");
//        } else {
//            System.out.println("没有可关闭的窗口句柄。");
//        }
//    }
//
////    public static void main(String[] args) throws IOException, InterruptedException {
////        WebDriver driver = new ChromeDriverUtil().getWebDriver();
////        String SEARCH_URL = "https://www.toutiao.com/article/7493164180275560960/";
////        driver.get(SEARCH_URL);
////        System.out.println("开始获取页面数据！！！---------------" + driver.getCurrentUrl());
////
////        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
////        // 等待文章标题元素可见 article-content
////        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".article-content")));
////
////        // 等待关键元素加载
//////            wait.until(d -> d.findElement(By.cssSelector(".article-title")));
////
////        // 获取文章信息
////        String title = driver.findElement(By.cssSelector(".article-content")).getText();
////
////
////        WebElement element = driver.findElement(By.xpath("//*[@id=\"root\"]/div[2]/div[2]/div[1]/div/div/div/div/h1"));
////        System.out.println(element.getText());
////        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
////
////
//////        WebElement element1 = driver.findElement(By.xpath("//*[@id=\"root\"]/div[2]/div[2]/div[1]/div/div/div/div/div[2]img/@src"));
////        List<WebElement> elements1 = driver.findElements(By.xpath("//*[@id=\"root\"]/div[2]/div[2]/div[1]/div/div/div/div/div[2]/article/div[1]/img"));
////
////        for (WebElement elementObj : elements1) {
////            System.out.println(elementObj.getAttribute("src")); // 打印每个元素的文本
////        }
////
////        List<WebElement> elements = driver.findElements(By.className("article-content"));
////        for (WebElement elementObj : elements) {
////            System.out.println(elementObj.getText()); // 打印每个元素的文本
////        }
////
////
//////        String content = driver.findElement(By.cssSelector(".article-content")).getText();
////
////        System.out.println("标题：" + title);
//////        System.out.println("内容：" + content.substring(0, Math.min(content.length(), 100)) + "...");
////
////
////        // todo 处理过程
////
////        ChromeDriverUtil.closeWindow(driver);
////        driver.quit(); // 关闭浏览器并退出 WebDriver 会话
////    }
//
//
//
//
////    public static void main(String[] args) {
////        // 自动配置浏览器驱动
////        io.github.bonigarcia.wdm.WebDriverManager.chromedriver().setup();
////        WebDriver driver = new ChromeDriverUtil().getWebDriver();
////        try {
////            driver.get("https://www.toutiao.com/article/7494643170295185930/?log_from=26dc8c5fa6a84_1745222405387");
////
////            // 滚动页面触发图片加载
////            ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
//////            Thread.sleep(2000);
////
////            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
////            // 等待文章标题元素可见 article-content
////            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".article-content")));
////
////            // 获取文章容器的HTML内容
////            String htmlContent = driver.findElement(By.cssSelector(".article-content"))
////                    .getAttribute("innerHTML");
////
////            System.out.println(htmlContent);
////            System.out.println("！！！！！！！！！！！！！！！！！！！！！！！");
////
////            // 使用JSoup解析HTML
////            Document doc = Jsoup.parse(htmlContent);
////
////            // 提取文本内容（清理HTML标签）
////            String cleanText = doc.text()
////                    .replaceAll("\\s{2,}", " ")
////                    .trim();
////
////            // 提取图片链接（处理懒加载）
////            List<String> imageUrls = new ArrayList<>();
////            Elements imgs = doc.select("img");
////            for (Element img : imgs) {
////                // 优先取data-src（处理懒加载）
////                String src = img.attr("data-src");
////                if (src.isEmpty()) {
////                    src = img.attr("src");
////                }
////                // 过滤无效链接
////                if (src.startsWith("http")) {
////                    imageUrls.add(src);
////                }
////            }
////
////            // 输出结果
//////            System.out.println("【文章正文】\n" + cleanText);
//////            System.out.println("\n【图片列表】");
//////            imageUrls.forEach(url -> System.out.println(" - " + url));
////
////        } catch (Exception e) {
////            e.printStackTrace();
////        } finally {
////            driver.quit();
////        }
////    }
//
//
//
//
//
//
//    public static void main(String[] args) throws InterruptedException {
//        // 自动配置浏览器驱动
//        WebDriverManager.chromedriver().setup();
//        WebDriver driver = new ChromeDriverUtil_wanzneg().getWebDriver();
//        try {
//            driver.get("https://www.toutiao.com/");
//
//            driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[5]/div[1]/div/div/div/div[1]/div/ul/li[9]")).click();
//            driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[5]/div[1]/div/div/div/div[1]/div/ul/li[9]/div[2]/ul/li[4]/div")).click();
//
//            WebDriverWait waitcard = new WebDriverWait(driver, Duration.ofSeconds(10));
//            waitcard.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".feed-card-wrapper")));
//
//            JavascriptExecutor js = (JavascriptExecutor) driver;
//            for (int i = 0; i <5; i++) {
//                js.executeScript("document.documentElement.scrollTop=10000");
//                Thread.sleep(1000);//有用别删
//            }
//            js.executeScript("document.documentElement.scrollTop=0");
//            js.executeScript("document.documentElement.scrollTop=340");
//
//            List<WebElement> element123 = driver.findElements(By.cssSelector(".feed-card-wrapper"));
//            System.out.println(element123.size());
//
//            Thread.sleep(1500);//有用，别删，否则element123获取不到数据
//            element123.forEach(webElement -> {
//                String titleStr = "";
////                List<WebElement> templist = webElement.findElements(By.cssSelector(".title"));
////                System.out.println(templist.size());
//                WebElement tttt=null;
//                try {
//                    tttt = webElement.findElement(By.cssSelector(".title"));
//                    titleStr = tttt.getText();
//                } catch (NoSuchElementException e) {
//                    System.out.println("NoSuchElementException↓↓↓↓");
//                    System.out.println(e.getMessage());
//                }
//                if(tttt == null ){
//                    tttt = webElement.findElement(By.cssSelector(".feed-card-cover"));
//                }
//                if(tttt == null){
//                    return;
//                }
//                System.out.println("tttttttttttttttttttttttttttttttttttt");
//                System.out.println(tttt.getText());
//
//
//
//
////                ((JavascriptExecutor) driver).executeScript("arguments[0].style.setProperty('transform', 'translateY(' + arguments[1] + 'px)');", webElement, -1 * webElement.getLocation().getY());
//
//                tttt.click();
////                webElement.findElement(By.cssSelector(".title")).click();
////                webElement.findElement(By.cssSelector(".feed-card-cover")).click();
//
////                System.out.println(webElement.getText());
////                Actions actions = new Actions(driver);
////                actions.moveToElement(webElement).click().perform();
//
//
////                webElement.click();
//
//                Set<String> allWindows = driver.getWindowHandles();
//                Iterator<String> iterator = allWindows.iterator();
//                String parentWindow = iterator.next(); // 父窗口句柄
//                String newWindow = iterator.next(); // 新窗口句柄
//                driver.switchTo().window(newWindow);
//
////            WebDriverWait waitNie = new WebDriverWait(driver, Duration.ofSeconds(10));
////            // 等待文章标题元素可见 article-content
////            waitNie.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".article-content")));
//
//            // 获取文章容器的HTML内容
//            String htmlContent = null;
//                try {
//                    WebElement element = driver.findElement(By.xpath("//*[@id=\"root\"]/div[2]/div[2]/div[1]/div/div/div/div/h1"));
//                    System.out.println(element.getText());
//
//                    htmlContent = driver.findElement(By.cssSelector(".article-content")).getAttribute("innerHTML");
//                } catch (NoSuchElementException e) {
//                    System.out.println("htmlContent----NoSuchElementException↓↓↓↓");
//                    System.out.println(e.getMessage());
//                }
//                if(htmlContent == null){
//                    htmlContent = driver.findElement(By.cssSelector(".weitoutiao-html")).getAttribute("innerHTML");
//                }
//
//            System.out.println(htmlContent);
//            System.out.println("！！！！！！！！！！！！！！！！！！！！！！！");
//
//            // 使用JSoup解析HTML
//            Document doc = Jsoup.parse(htmlContent);
//
//            // 提取文本内容（清理HTML标签）
//            String cleanText = doc.text()
//                    .replaceAll("\\s{2,}", " ")
//                    .trim();
//
//            // 提取图片链接（处理懒加载）
//            List<String> imageUrls = new ArrayList<>();
//            Elements imgs = doc.select("img");
//            for (Element img : imgs) {
//                // 优先取data-src（处理懒加载）
//                String src = img.attr("data-src");
//                if (src.isEmpty()) {
//                    src = img.attr("src");
//                }
//                // 过滤无效链接
//                if (src.startsWith("http")) {
//                    imageUrls.add(src);
//                }
//            }
//                    driver.close();
//                    driver.switchTo().window(parentWindow);
//                try {
//                    System.out.println("driver.getCurrentUrl()");
//                    System.out.println(driver.getCurrentUrl());
//                    Thread.sleep(500);
////                    JavascriptExecutor jsNei = (JavascriptExecutor) driver;
////                    jsNei.executeScript("document.documentElement.scrollTop=176");
////                    ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,176)");
//                    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", webElement);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            });
//
//
//
//
//
//
//
//
//
//
//
//
//
//
////            String htmlContentList = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[5]/div[1]/div/div/div/div[2]")).getAttribute("innerHTML");
//
//
//
////            WebElement element = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[5]/div[1]/div/div/div/div[1]/div/ul/li[9]/div[2]/ul/li[4]/div"));
////            System.out.println(element.getText());
////            driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[5]/div[1]/div/div/div/div[2]")).click();
//
//
////            // 获取文章容器的HTML内容
////            String htmlContent = driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[5]/div[1]/div/div/div/div[2]"))
////                    .getAttribute("innerHTML");
////
////            System.out.println(htmlContent);
////            System.out.println("！！！！！！！！！！！！！！！！！！！！！！！");
////
////            // 使用JSoup解析HTML
////            Document doc = Jsoup.parse(htmlContent);
////
////            // 提取文本内容（清理HTML标签）
////            String cleanText = doc.text()
////                    .replaceAll("\\s{2,}", " ")
////                    .trim();
////
////            // 提取图片链接（处理懒加载）
////            List<String> imageUrls = new ArrayList<>();
////            Elements imgs = doc.select("img");
////            for (Element img : imgs) {
////                // 优先取data-src（处理懒加载）
////                String src = img.attr("data-src");
////                if (src.isEmpty()) {
////                    src = img.attr("src");
////                }
////                // 过滤无效链接
////                if (src.startsWith("http")) {
////                    imageUrls.add(src);
////                }
////            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            Thread.sleep(180000);
//            driver.quit();
//        }
//    }
//}
